package com.maheimei.controller;

import com.alibaba.fastjson.JSON;
import com.maheimei.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;

@CrossOrigin
@Controller
public class UserController {
    //原生参数接收
    @RequestMapping("/index1")
    public String show1(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String username = request.getParameter("username");
        String age = request.getParameter("age");
        System.out.println(username);
        System.out.println(age);
        return "index.html";
    }
    //形参接收
    @RequestMapping("/index2")
    public String show2(String username,int age){
        System.out.println(username);
        System.out.println(age+5);
        System.out.println("jj");
        return "index.html";

    }
    /**
     * @RequestParam注解
     * @param username
     * @param age
     * @return
     * 在页面中传递的参数key与方法形参名不一致，可以通过@RequestParam注解来指定参数名，但是注意如果形参名一直，则无需指定@RequestParam注解，直接使用形参接收即可
     *
     */
    @RequestMapping("/index3")
    public String show3(@RequestParam("name") String username,@RequestParam("userage") int age){
        System.out.println(username);
        System.out.println(age+5);
        return "index.html";
    }
    //形参接收对象
    @RequestMapping("/index4")
    public String show4(User u){
        System.out.println("----index4");
        System.out.println(u.getUsername());
        System.out.println(u.getAge());
        return "index.html";
    }
    //形参接受对象，同时接受一个遍历
    @RequestMapping("/index5")
    public String show5(User u,String hobby){
        System.out.println("----index5");
        System.out.println(u.getUsername());
        System.out.println(u.getAge());
        System.out.println(hobby);
        return "index.html";
    }
    //形参接受数组
    @ResponseBody
    @RequestMapping("/index6")
    public String show6(String[] hobby){
        System.out.println("----index6");
        System.out.println(Arrays.toString(hobby));

        return "传递数组";
    }
    //形参接收集合
    @RequestMapping("/index7")
    public String show7(@RequestParam("hobby") ArrayList<String> hobby){
        System.out.println("----index7");
        System.out.println(hobby.toString());
        return "index.html";
    }
    //形参接受对象集合
    @RequestMapping("/index8")
    public String show8(@RequestBody ArrayList<User> users){
        System.out.println("-----index8");
        System.out.println(users.toString());
        return "index.html";
    }
    @RequestMapping("/index9")
    public String show9(User u){
        System.out.println("-----index9");
        System.out.println(u.toString());
        return "forward:index.html";
    }
    //使用redirect重定向
    @RequestMapping("/index10")
    public String show10(User u){
        System.out.println("-----index10");
        return "redirect:index.html";
    }
    //使用ModelAndView作为响应内容
    @RequestMapping("/index11")
    public ModelAndView show11(User u){
        ModelAndView mav = new ModelAndView();
        mav.addObject("user",u);
        mav.setViewName("index.html");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/index12")
    public String show12(User u){
        //JSON.parseObject("前端或数据库取出的JSON字符串"，User.class);JSON转对象，通常是后台用
        User user = JSON.parseObject("{\"username\":\"张三\",\"age\":18}",User.class);
        System.out.println(user.getUsername());
        System.out.println(user.getAge());
        return JSON.toJSONString(u);
    }

}
